package com.typing.service.impl;

import com.typing.entity.AdminEntity;
import com.typing.mapper.AdminMapper;
import com.typing.service.AdminService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {

	@Resource
	AdminMapper adminMapper;

	@Override
	public AdminEntity adminLogin(AdminEntity admin) {
		//	拿数据库的盐与密码进行MD5加密的出新密码
		String salt = adminMapper.getSalt(admin.getUsername());
		SimpleHash simpleHash = new SimpleHash("MD5", admin.getPassword(), salt, 3);
		String newPassword = simpleHash.toHex();
		admin.setPassword(newPassword);
		return adminMapper.selectAdmin(admin);
	}

	@Override
	public int addAdmin(AdminEntity admin) {
		//	拿数据库的盐与密码进行MD5加密的出新密码
		String salt = DigestUtils.md5DigestAsHex(UUID.randomUUID().toString().getBytes());
		SimpleHash simpleHash = new SimpleHash("MD5", admin.getPassword(), salt, 3);
		String newPassword = simpleHash.toHex();
		admin.setPassword(newPassword);
		admin.setSalt(salt);
		return adminMapper.addAdmin(admin);
	}

	@Override
	public AdminEntity isRegister(AdminEntity admin) {
		return adminMapper.isRegister(admin);
	}

	@Override
	public int setTag(AdminEntity admin) {
		return adminMapper.setTag(admin);
	}

	@Override
	public AdminEntity selectTag(String tag) {
		return adminMapper.selectTag(tag);
	}

	@Override
	public AdminEntity getNewAdmin(AdminEntity admin) {
		return adminMapper.selectAdmin(admin);
	}
}
